package cn.kais.drawer.unit;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 截取视频封面原图
 */
public class UnitVideo {

    /**
     * 测试
     */
    public void test() {
//        String uri = "https://bowtz-lcd.oss-cn-hangzhou.aliyuncs.com/res/lcd/material/video/1806211386873565184/1806211387284606978/20240708/8d67e445a5824019b2d572559009f9dc.mp4";
        String uri = "https://vdept3.bdstatic.com/mda-qg3d6avs4mw3wr6h/360p/h264/1720085179317796412/mda-qg3d6avs4mw3wr6h.mp4?v_from_s=hkapp-haokan-nanjing&auth_key=1731582935-0-0-b3f33087e1c83b3529efb8530d5d769a&bcevod_channel=searchbox_feed&pd=1&cr=0&cd=0&pt=3&logid=0935763906&vid=6501515071485153767&klogid=0935763906&abtest=";
        BufferedImage ima = iCoverBuffer(uri);
        if (null == ima) {
            System.out.println("未获取到视频封面流信息");
            return;
        }
        try {
            System.out.println("宽度：" + ima.getWidth());
            System.out.println("高度：" + ima.getHeight());
            ImageIO.write(ima, "png", new File("VideoCover.png"));
        } catch (IOException e) {
            System.out.println("视频封面流信息输出图片出错");
            throw new RuntimeException(e);
        }
    }

    /**
     * 当前视频 封面流资源
     *
     * @param uri 视频地址
     */
    public static BufferedImage iCoverBuffer(String uri) {
        try (FFmpegFrameGrabber iGrabber = new FFmpegFrameGrabber(uri)) {
            iGrabber.start();
            System.out.println("总帧数：" + iGrabber.getLengthInVideoFrames()); // 总帧数
            System.out.println("总秒数：" + iGrabber.getLengthInTime() / (1000 * 1000)); // 多少秒
            System.out.println("每秒帧数：" + iGrabber.getVideoFrameRate()); // 每秒多少帧
            System.out.println("视频宽度：" + iGrabber.getImageWidth()); // 视频宽度
            System.out.println("视频高度：" + iGrabber.getImageHeight()); // 视频高度
            System.out.println("视频编码：" + iGrabber.getVideoCodecName());
            System.out.println("音频音道：" + iGrabber.getAudioChannels());
            System.out.println("音频编码：" + iGrabber.getAudioCodecName());
            // 视频总帧数
            int framesSize = iGrabber.getLengthInFrames();
            if (framesSize > 0) {
                // 设置需要获取的帧数下标（封面图帧下标），不设置取第一帧
                // 通常取中间帧作为封面
//              iGrabber.setFrameNumber(framesSize / 2);
//                Frame frame = iGrabber.grab();
                Frame frame = iGrabber.grabImage();
//                Frame frame = iGrabber.grabFrame();
                return iFrame2Buffer(frame);
            }
            try {
                iGrabber.stop();
            } catch (FrameGrabber.Exception e) {
                System.out.println("关闭视频资源出错");
                throw new RuntimeException(e);
            }
            return null;
        } catch (FrameGrabber.Exception e) {
            System.out.println("读取视频资源出错");
            throw new RuntimeException(e);
        }
    }

    /**
     * 帧资源转换流资源
     *
     * @param frame 帧
     * @return 流
     */
    private static BufferedImage iFrame2Buffer(Frame frame) {
        try (Java2DFrameConverter converter = new Java2DFrameConverter()) {
            return converter.convert(frame);
        } catch (Exception e) {
            System.out.println("转换视频帧资源出错");
            throw new RuntimeException(e);
        }
    }

}
